<template>
  <div class="box">
    <el-row>
      <el-col :span="22" :push="1">
        <div class="grid-content bg-purple">
          <div class="box_text">
            {{ title }}
            <el-divider></el-divider>
          </div>
          <div style="margin-top: 20px; margin-left: 40px; width: 500px">
            <el-form ref="form" :model="form" label-width="80px">
              <el-form-item label="车辆名称">
                <el-input
                  v-model="form.carName"
                  placeholder="请输入内容"
                  @blur="setcarBrank(form.carName)"
                  :disabled="flag"
                ></el-input>
              </el-form-item>
              <el-form-item label="车牌号">
                <el-input
                  v-model="form.carNumber"
                  placeholder="请输入内容"
                  :disabled="flag"
                ></el-input>
              </el-form-item>
              <el-form-item label="品牌">
                <el-select v-model="form.carBrank" disabled>
                  <el-option
                    v-for="item in options"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="车系">
                <el-input
                  v-model="form.carSeries"
                  placeholder="请输入内容"
                  :disabled="flag"
                ></el-input>
              </el-form-item>
              <el-form-item label="燃油类型">
                <el-select v-model="form.fuelOiltype" placeholder="请选择" :disabled="flag">
                  <el-option label="燃油" value="0"></el-option>
                  <el-option label="电动" value="1"></el-option>
                  <el-option label="油电混合" value="2"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="座位数">
                <el-input
                  v-model="form.seating"
                  placeholder="请输入内容"
                  :disabled="flag"
                ></el-input>
              </el-form-item>
              <el-form-item label="备注">
                <el-input type="textarea" v-model="form.carRemark" :disabled="flag"></el-input>
              </el-form-item>
              <el-form-item label="上传图片">
                <el-upload
                  class="avatar-uploader"
                  action="http://localhost:8030/ftp/upload"
                  :show-file-list="false"
                  :on-success="handleAvatarSuccess"
                  :before-upload="beforeAvatarUpload"
                  :headers="upheaders"
                  :disabled="flag"
                >
                  <img v-if="imageUrl" :src="form.carImg" class="avatar" />
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                  <div slot="tip" class="el-upload__tip">
                    只能上传jpg/png文件，且不超过500kb
                  </div>
                </el-upload>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="onSubmit" :disabled="flag">立即提交</el-button>
                <el-button @click="back">取消</el-button>
              </el-form-item>
            </el-form>
          </div>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import axios from "axios";
export default {
  data() {
    return {
      form: {
        id: this.$route.query.id,
        carImg: "",
      },
      title: "添加车辆",
      imageUrl: false,
      upheaders: {
        Authorization: "Bearer" + localStorage.getItem("token"),
      },
      flag:false,
      options: [
        { value: "奥迪", labal: "0" },
        { value: "宝马", labal: "1" },
        { value: "奔驰", labal: "2" },
        { value: "其他", labal: "3" },
      ],
    };
  },
  created() {
    if (this.form.id != undefined) {
      this.imageUrl=true
      this.title="修改车辆信息"
      this.getCarById(this.form.id);
    }
    if(this.form.id != undefined && this.$route.query.flag!=undefined ) {
      this.imageUrl=true
    this.title="车辆信息"
      this.flag = true
      this.getCarById(this.form.id);
    }
  },
  methods: {
    back(){
      this.$router.go(-1)
    },
    // 回显车辆信息
    getCarById(id) {
      axios.post("/car/getCarById?id=" + id).then((res) => {
        console.log(res);
        if (res.data.code === 200) {
          this.form = res.data.data;
        } else {
          this.$message.error("查询车辆信息失败");
        }
      });
    },
    handleAvatarSuccess(res, file) {
      console.log(res);
      if (res != "上传失败") {
        this.$message.success("上传成功");
        this.form.carImg = res;
        this.imageUrl = true;
      } else {
        this.imageUrl = false;
        this.$message.error(res);
      }
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg" || "image/png";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG或者png 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 500kb!");
      }
      return isJPG && isLt2M;
    },
    // 添加或者修改 方法
    onSubmit() {
      if (this.form.id != undefined) {
        axios.post("/car/updateCar", this.form).then((res) => {
          console.log(res);
          if (res.data.code === 200) {
            this.$message.success(res.data.data);
            this.$router.go(-1)
          } else {
            this.$message.error(res.data.data);
          }
        });
      } else {
        axios.post("/car/addCar", this.form).then((res) => {
          console.log(res);
          if (res.data.code === 200) {
            this.$message.success(res.data.data);
            this.$router.go(-1)
          } else {
            this.$message.error(res.data.data);
          }
        });
      }
    },
    setcarBrank(val) {
      this.form.carBrank = "";
      this.options.forEach((e) => {
        if (String(val).includes(String(e.value))) {
          console.log("111");
          this.form.carBrank = e.value;
        }
      });
      if (this.form.carBrank == "") {
        this.form.carBrank = "其他";
      }
    },
  },
};
</script>

<style scoped>
/* element-ui样式 */
.el-row {
  margin-bottom: 10px;

  &:last-child {
    margin-bottom: 0;
  }
}

.el-col {
  border-radius: 4px;
}

.bg-purple {
  background: #fff;
  border-radius: 10px;
}

/* 自定义样式 */
/* * {
    margin: 0px;
    padding: 0px;
  } */
.box {
  width: 98%;
  height: 100%;
  margin: auto;
  /* background-color: red; */
}

.box_text {
  padding-top: 20px;
  /* padding-left: 40px; */
  margin-left: 40px;
  font-size: 16px;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 130px;
  height: 130px;
  line-height: 130px;
  text-align: center;
  border: 1px solid #6f6f6f;
}

.avatar {
  width: 130px;
  height: 130px;
  display: block;
}
</style>